package cn.lf.android.lawfirm.notice;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.ChangeApprovalActivity;
import cn.lf.android.lawfirm.changeapproval.ChangeApprovalListActivity;
import cn.lf.android.lawfirm.distribution.DistributionActivity;
import cn.lf.android.lawfirm.home.controller.MessageController;
import cn.lf.android.lawfirm.home.modle.Message;
import cn.lf.android.lawfirm.notice.controller.MessageReadController;
import cn.lf.android.lawfirm.notice.view.NoticeAdapter;
import cn.lf.android.lawfirm.outgoingregistration.OutGoingInfoActivity;
import cn.lf.android.lawfirm.undertake.approval.UndertakeActivity;
import cn.lf.android.lawfirm.upgrade.approval.UpgradeActivity;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;
import cn.lf.android.lawfirm.worklog.AddWorkLogActivity;
import cn.lf.android.lawfirm.worklog.WorkLogSearchListActivity;

import static android.R.id.list;
import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.eb_tv_right;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.rl_search;

public class NoticeListActivity extends OrdinaryBaseActivity
        implements MessageController.MessageCallback, MessageReadController.MessageReadCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private NoticeAdapter mNoticeAdapter;
    List<Message> mbeans = new ArrayList<>();

    private MessageController mMessageController;
    private int pageIndex = 1;

    public static Boolean isRefresh = false;
    public MessageReadController mMessageReadController;

    @Override
    public int getChildView() {
        return R.layout.activity_notice_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mNoticeAdapter = new NoticeAdapter(this, mbeans);
        mListView.setAdapter(mNoticeAdapter);
        mSearchLayout.setVisibility(View.GONE);
        mSearch.setVisibility(View.GONE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case eb_tv_right:
                Intent intent1 = new Intent(this, AddWorkLogActivity.class);
                startActivity(intent1);
                break;
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, WorkLogSearchListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("消息通知");
        tvSearch.setVisibility(View.GONE);
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                mbeans.clear();
                mMessageController.onMessage(String.valueOf(pageIndex), "notYet");
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mMessageController.onMessage(String.valueOf(pageIndex), "notYet");
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                if ("承办审批".equals(mbeans.get(position).Title)) {
                    intent.setClass(NoticeListActivity.this, UndertakeActivity.class);
                    intent.putExtra("caseID", mbeans.get(position).Extras.caseID);
                    intent.putExtra("appId", mbeans.get(position).Extras.relationSID);
                    startActivity(intent);
                } else if ("合同变更".equals(mbeans.get(position).Title)) {
                    intent.setClass(NoticeListActivity.this, ChangeApprovalActivity.class);
                    intent.putExtra("caseID", mbeans.get(position).Extras.caseID);
                    intent.putExtra("relationSid", mbeans.get(position).Extras.relationSID);
                    startActivity(intent);
                } else if ("律师分派".equals(mbeans.get(position).Title)) {
                    intent.setClass(NoticeListActivity.this, DistributionActivity.class);
                    intent.putExtra("caseID", mbeans.get(position).Extras.caseID);
                    startActivity(intent);
                } else if ("新增审级".equals(mbeans.get(position).Title)) {
                    intent.setClass(NoticeListActivity.this, UpgradeActivity.class);
                    intent.putExtra("id", mbeans.get(position).Extras.relationSID);
                    intent.putExtra("caseID", mbeans.get(position).Extras.caseID);
                    startActivity(intent);
                } else if ("外出登记".equals(mbeans.get(position).Title)) {
                    intent.setClass(NoticeListActivity.this, OutGoingInfoActivity.class);
                    intent.putExtra("id", mbeans.get(position).Extras.relationSID);
//                    intent.putExtra("id", mbeans.get(position).Extras.caseID);
                    startActivity(intent);
                }

//                Intent intent = new Intent(ChangeApprovalListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);

            }
        });
    }

    @Override
    public void initData() {
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mMessageController = new MessageController(this, this);
        mMessageController.onMessage(String.valueOf(pageIndex), "notYet");
        mMessageReadController = new MessageReadController(this, this);
    }


    public void doKnow(int position) {
        mMessageReadController.onMessageRead(mbeans.get(position).sid);
    }

    @Override
    public void onMeaasgeSuccessed(List<Message> beans) {
        mbeans.addAll(beans);
        mNoticeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onMessageFailed() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }

    @Override
    public void onMessageReadSuccessed(String str) {
        refreshLayout.autoRefresh();
    }

    @Override
    public void onMessageReadFailed() {

    }
}

